package com.itheima.reggie.ctrl;/**
 * @author zwx
 * @date 2023/11/16 16:48
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.model.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zwx
 * @date 2023/11/16 16:48
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart, HttpServletRequest request){
        log.info("传来的购物车信息为：{}",shoppingCart);
        //可以通过session拿到当前用户的userId，也可以通过封装的BaseContext.getCurrentId()方法来拿到当前userId
//        HttpSession session = request.getSession();
//        Object userId = session.getAttribute("user");

        //设置用户id，表示当前购物车是哪个用户的
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        //查询一下当前添加的菜品或套餐是否在购物车中
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        wrapper.eq(shoppingCart.getDishId() != null,ShoppingCart::getDishId,shoppingCart.getDishId());
        wrapper.eq(shoppingCart.getSetmealId() != null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart one = shoppingCartService.getOne(wrapper);


        if (one != null){
            //如果已经存在，则在原来数量上加1
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
        }else{
            //如果不存在，则添加到购物车，数量默认就是1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }

        return R.success(one);
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车...");
        //获取当前登录用户的购物车
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        wrapper.orderByAsc(ShoppingCart::getCreateTime);//根据创建时间升序排序
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }


    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("减少购物车菜品or套餐的number...");
        //减一
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        wrapper.eq(shoppingCart.getDishId() != null,ShoppingCart::getDishId,shoppingCart.getDishId());
        wrapper.eq(shoppingCart.getSetmealId() != null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart one = shoppingCartService.getOne(wrapper);

        if(one != null){
            Integer number = one.getNumber();
            if (number <= 0){
                shoppingCartService.remove(wrapper);
            }else {
                one.setNumber(number - 1);
            }
            shoppingCartService.updateById(one);
        }

        return null;
    }


    @DeleteMapping("/clean")
    public R<String> clean(){
        log.info("清空购物车...");

        Long userId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userId != null,ShoppingCart::getUserId,userId);
        shoppingCartService.remove(wrapper);

        return R.success("购物车清空成功");
    }



}
